package com.netease.yunxin.kit.chatkit.ui.view.useful

import android.content.Context
import android.os.Handler
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.compass.framework.ext.onClick
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.netease.yunxin.kit.alog.ALog
import com.netease.yunxin.kit.chatkit.ui.ChatKitUIConstant
import com.netease.yunxin.kit.chatkit.ui.databinding.CustomUsefulExpressionsBinding
import com.netease.yunxin.kit.chatkit.ui.model.UsefulExpressions

/**
 * Description： 自定义常用语界面
 * Date:2023/12/4 9:49
 */
class UsefulExpressionsView : LinearLayout {
    private var expressionsBinding: CustomUsefulExpressionsBinding? = null
    private var listener: IExpressionClickListener? = null

    private var context: Context
    private lateinit var mAdapter: UsefulExpressionsAdapter
    private var loaded = false
    private var uiHandler: Handler? = null

    val list: MutableList<UsefulExpressions> = mutableListOf()
    constructor(context: Context) : super(context) {
        this.context = context
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        this.context = context
        initView(context)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        this.context = context
        initView(context)
    }

    //初始化UI，可根据业务需求设置默认值。
    private fun initView(context: Context) {
        uiHandler = Handler(context.mainLooper)
        expressionsBinding =
            CustomUsefulExpressionsBinding.inflate(LayoutInflater.from(context), this, true)
        mAdapter = UsefulExpressionsAdapter()
        expressionsBinding?.rcyExpression?.apply {
            val layoutFelManager = FlexboxLayoutManager(context)
            layoutFelManager.flexWrap = FlexWrap.WRAP // 按正常方向换行
            layoutFelManager.flexDirection = FlexDirection.ROW // 水平方向
            layoutFelManager.justifyContent = JustifyContent.FLEX_START // 左对齐
            layoutManager = layoutFelManager
            adapter = mAdapter
            setUsefulData()
        }
        mAdapter.onItemClickListener={ view: View, position: Int ->
            listener?.onExpressionClick(1,list[position].name)
        }
        expressionsBinding?.tvExpressBack?.onClick {
            listener?.onExpressionClick(4,"返回")
        }
        expressionsBinding?.tvAddExpression?.onClick {
            listener?.onExpressionClick(2,"添加")
        }
        expressionsBinding?.tvManagementExpression?.onClick {
            listener?.onExpressionClick(3,"管理")
        }
    }

    fun setUsefulData() {

        list.add(UsefulExpressions(1, "您好，我是交大一附院刑红璇医生。"))
        list.add(UsefulExpressions(2, "我的电话：15686207825"))
        list.add(UsefulExpressions(3, "好的好的"))
        list.add(UsefulExpressions(4, "我这会没空，下午有空"))
        list.add(UsefulExpressions(5, "请您上传病历吧"))
        list.add(UsefulExpressions(5, "好的"))
        list.add(UsefulExpressions(5, "标准写法"))
        list.add(UsefulExpressions(5, "达玛西亚"))
        list.add(UsefulExpressions(5, "台南市"))
        list.add(UsefulExpressions(5, "天使"))
        list.add(UsefulExpressions(5, "梅西"))
        list.add(UsefulExpressions(5, "大马丁"))
        list.add(UsefulExpressions(5, "C罗"))
        list.add(UsefulExpressions(5, "您好，欢迎来参加我的会诊"))

        mAdapter.setData(list)

    }



    fun setListener(listener: IExpressionClickListener?) {
        listener?.let {
            this.listener = listener
        } ?: kotlin.run {
            ALog.d(ChatKitUIConstant.LIB_TAG, "expressions", "listener is null")
        }
    }

    fun loadUsefulExpressions() {

    }

}